package com.jushu.storbox.manager;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import android.content.Context;

import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.database.DAOFactory;
import com.jushu.storbox.entities.DownloadObject;
import com.jushu.storbox.task.tqueue.DownloadTask;
import com.jushu.storbox.task.tqueue.PriorityTaskScheduler;
import com.jushu.storbox.util.LogUtil;

public class DownloadTaskManager extends BaseDownloadManager{
	private static final int POOL_SIZE = 2;
	private static final Set<DownloadTask> downloadSet = new HashSet<DownloadTask>();//all
	private static final PriorityTaskScheduler<DownloadTask> downloadSequence = new PriorityTaskScheduler<DownloadTask>(POOL_SIZE);
	private static final Set<DownloadTask> failedSet = new HashSet<DownloadTask>();//fail
	private static final Set<DownloadTask> runningSet = new HashSet<DownloadTask>();//running
	private static final Set<DownloadTask> waitingSet = new HashSet<DownloadTask>();//waiting
	
	public static Set<DownloadTask> getAllTaskes() {
		return  new HashSet<DownloadTask>(downloadSet);
	}
	
	public static Set<DownloadTask> getAllRunningTaskes() {
		return  new HashSet<DownloadTask>(runningSet);
	}
	
	public static Set<DownloadTask> getAllWaitingTaskes() {
		return  new HashSet<DownloadTask>(waitingSet);
	}
	
	public static Set<DownloadTask> getAllFailTasks() {
		return  new HashSet<DownloadTask>(failedSet);
	}
	
	public static void stop() {
		downloadSequence.stop();
	}
	
	public static void start() {
		downloadSequence.start();
	}
	
	public static Set<DownloadTask> getAllTaskes(String folderID) {
		Set<DownloadTask> tasks = new HashSet<DownloadTask>();
		for(Iterator<DownloadTask> itor = downloadSet.iterator(); itor.hasNext();) {
			DownloadTask task = itor.next();
			if (folderID.equalsIgnoreCase(task.getFolderID()))
				tasks.add(task);
		}
		return tasks;
	}
	
	public static boolean addTask(DownloadTask task) {
		synchronized (downloadSet) {
			if (null == task.getId() || "".equalsIgnoreCase(task.getId().trim())) {
				LogUtil.d("DownloadTaskManager", "add task failure! becouse id is emputy");
				return false;
			}
			if (/*10 >= downloadSet.size() && */downloadSet.add(task) && waitingSet.add(task)) {
				LogUtil.i("DownloadTaskManager", "before add Size is: "+downloaddingSize);
				addDownloaddingSize(task.getFileSize());
				downloadSequence.runTask(task);
				LogUtil.d("DownloadTaskManager", "add task success!");
				return true;
			} else {
				LogUtil.d("DownloadTaskManager", "add task failure!");
				return false;
			}
		}
	}
	
	public static void startTask(Context context, DownloadObject downItem) {
		if(downItem == null) {
			return ;
		}
		DownloadTask tempTask = getTask(downItem.getId());
		if(tempTask == null) {
			addTask(new DownloadTask(context, 0, null, downItem, downItem.isSync()));
		}
	}
	
	public static void cancelTask(Context context, DownloadObject downItem) {
		if(downItem == null) {
			return ;
		}
		DownloadTask tempTask = getTask(downItem.getId());
		if(tempTask != null) {
			tempTask.cancelDownloadTask();
			removeTask(tempTask);
		} else {
			DAOFactory.instance(context).getDownloadFileDao()
				.deleteFile(downItem.getId(), downItem.getOwnerBy());
		}
	}
	
	public static void stopTask(Context context, DownloadObject downItem) {
		if(downItem == null) {
			return ;
		}
		DownloadTask tempTask = getTask(downItem.getId());
		if(tempTask != null) {
			tempTask.stopDownloadTask();
			removeTask(tempTask);
		} else {
			downItem.setTransStatus(Constant.TRANSLATE_IS_STOPED);
			downItem.notifyChange();
			DAOFactory.instance(context).getDownloadFileDao()
				.updateDownloadObject(downItem);
		}
	}
	
	public static DownloadTask getTask(String id) {
		DownloadTask task = null;
		for(Iterator<DownloadTask> itor = downloadSet.iterator(); itor.hasNext();) {
			task = itor.next();
			if (id.equalsIgnoreCase(task.getId()))
				return task;
		}
		return null;
	}
	
	public static DownloadTask getFailureTask(String id) {
		DownloadTask task = null;
		for(Iterator<DownloadTask> itor = failedSet.iterator(); itor.hasNext();) {
			task = itor.next();
			if (id.equalsIgnoreCase(task.getId()))
				return task;
		}
		return null;
	}
	
	public static boolean isRunning(DownloadTask task) {
		synchronized (runningSet) {
			return runningSet.contains(task);
		}
	}
	
	public static boolean contains(DownloadTask task) {
		synchronized (downloadSet) {
			return downloadSet.contains(task);
		}
	}
	
	public static boolean isWaiting(DownloadTask task) {
		synchronized (waitingSet) {
			return waitingSet.contains(task);
		}
	}
	
	public static boolean removeTask(DownloadTask task) {
		LogUtil.i("DownloadTaskManager", "Task Removed Begin!");
		downloadSequence.removeTask(task);
		removeRunningTask(task);
		removeWaitingTask(task);
		boolean isRemoveSuccess = false;
		synchronized (downloadSet) {
			isRemoveSuccess = downloadSet.remove(task);
			if(isRemoveSuccess){
				subDownloaddingSize(task.getFileSize());
				LogUtil.i("DownloadTaskManager", "after remove Size  is: "+downloaddingSize);
			}
			return isRemoveSuccess;
		}
	}
	//
	public static boolean addRunningTask(DownloadTask task) {
		synchronized (runningSet) {
			return runningSet.add(task);
		}
	}
	
	public static boolean removeRunningTask(DownloadTask task) {
		synchronized (runningSet) {
			return runningSet.remove(task);
		}
	}
	
	public static boolean addFailTask(DownloadTask task) {
		synchronized (failedSet) {
//			task.deleteSelfFile();
			return failedSet.add(task);
		}
	}
	
	public static boolean removeFailTask(DownloadTask task) {
		synchronized (failedSet) {
			return failedSet.remove(task);
		}
	}
	
	//
	public static boolean addWaitingTask(DownloadTask task) {
		synchronized (waitingSet) {
			return waitingSet.add(task);
		}
	}
	
	public static boolean removeWaitingTask(DownloadTask task) {
		synchronized (waitingSet) {
			return waitingSet.remove(task);
		}
	}

	public static void cancelAllTask() {
		for (DownloadTask task : downloadSet) {
			task.stopDownloadTask();
		}
	}
}
